package com.yb.bussiness.announcement;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgAnnouncement;

public class AnnouncementServiceImpl implements AnnouncementService {

	private IBaseDao<TgAnnouncement, Long> dao;

	public AnnouncementServiceImpl() {
		dao = new BaseDao<TgAnnouncement, Long>(TgAnnouncement.class);
	}

	public boolean deleteByIdString(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "delete from t_g_announcement where id in (" + idString
				+ ")";
		try {
			dao.delete(sql);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public Map<String, Object> findAll(TgAnnouncement formBean, String page,
			String row) {
		String sql = "select *  from t_g_announcement n where 1=1  ";
		String countSql = "select count(n.id) from t_g_announcement n where 1=1 ";
		if (formBean != null) {
			if (formBean.getCategoryId() != 0) {
				sql += " and n.categoryid = " + formBean.getCategoryId();
				countSql += " and n.categoryid = " + formBean.getCategoryId();
			}
			if (formBean.getPcategoryId() != 0) {
				sql += " and n.PcategoryId = " + formBean.getPcategoryId();
				countSql += " and n.PcategoryId = " + formBean.getPcategoryId();
			}
			if (formBean.getType() != null && !"".endsWith(formBean.getType())) {
				sql += " and n.type = " + formBean.getType();
				countSql += " and n.type = " + formBean.getType();
			}
		}
		if (page != null && row != null) {
			int snum = Integer.parseInt(page) - 1;
			sql += " order by id desc   limit  "
					+ (snum * Integer.parseInt(row)) + "," + row + " ";
		}
		List<TgAnnouncement> list = dao.findList(sql);

		int total = 0;
		if (list.size() != 0) {
			total = Integer
					.parseInt((dao.findObjectArryList(countSql).get(0))[0] + "");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", total);
		return map;
	}

	public TgAnnouncement findTgByType(String type) {
		return dao.uniqueResult(
				" SELECT *  FROM t_g_announcement WHERE  TYPE=? ",
				new Object[] { type });
	}

	public TgAnnouncement findTribuneByType(String type) {
		return dao
				.uniqueResult(
						" SELECT *  FROM t_g_announcement WHERE  TYPE=? ORDER BY createDate desc ",
						new Object[] { type });
	}

	public TgAnnouncement read(Long id) {
		return dao.read(id);
	}

	public void saveOrUpdate(TgAnnouncement tgAnnouncement) {
		dao.saveOrUpdate(tgAnnouncement);
	}

	public TgAnnouncement findByCategory(Long categoryId, String type) {
		String sql = "select * from t_g_announcement where categoryid = ? and type = ? order by createDate desc ";
		return dao.uniqueResult(sql, new Object[] { categoryId, type });
	}

	public Integer findNewInfo(String nowTime) {
		String sql = "select count(*) from t_s_sysuser where createDate > '"
				+ nowTime + "'";
		Integer count = 0;
		count = bulidResult(sql);
		if (count == 0) {
			sql = "SELECT COUNT(*) FROM t_p_buy_record WHERE createTime > '"
					+ nowTime + "'";
			count = bulidResult(sql);
			if (count == 0) {
				sql = "SELECT COUNT(*) FROM t_g_brand_member WHERE createTime > '"
					+ nowTime + "'";
				count = bulidResult(sql);
			}
		}
		return count;
	}

	private Integer bulidResult(String sql) {
		Integer count = 0;
		try {
			List<Object[]> countList = dao.findObjectArryList(sql);
			Object[] countAry = countList.get(0);
			count = new Integer(countAry[0].toString());
		} catch (Exception e) {
			count = 0;
		}
		return count;
	}

}
